import { Breadcrumb, Button } from 'antd';
import React, { FC } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { PageContainer } from '@/component/page-container';
import styles from './index.less';
import Basic from '../component/basic';
import { NewTeachersSrv } from '@/service/teacher';
import { getPathRoot } from '@/utils';

const Groups: FC = () => {
  const navigate = useNavigate();
  const { id } = useParams();
  const [form, setForm] = React.useState({ basicTop: {}, basicBody: {} });

  const init = async () => {
    const resD = await NewTeachersSrv.detail(id);
    if (resD.success) {
      setForm(prevForm => ({
        ...prevForm,
        basicTop: { ...resD.data },
      }));
    }
    const resI = await NewTeachersSrv.info(id);
    if (resI.success) {
      if (resI.data.current_post_level) {
        resI.data.current_post_level = resI.data.current_post_level.split(',');
      }
      if (resI.data.health_condition) {
        resI.data.health_condition = resI.data.health_condition.split(',');
      }
      if (resI.data.highest_education) {
        resI.data.highest_education = resI.data.highest_education.split(',');
      }
      if (resI.data.current_post_type) {
        resI.data.current_post_type = resI.data.current_post_type.split(',');
      }
      if (resI.data.source_of_staff) {
        resI.data.source_of_staff = resI.data.source_of_staff.split(',');
      }
      console.log('🚀 ~ init ~ resI.data:', resI.data);

      setForm(prevForm => ({
        ...prevForm,
        basicBody: { ...resI.data },
      }));
    }

    console.log('form', form);
  };

  React.useEffect(() => {
    if (!id) {
      navigate(`${getPathRoot()}/onboarding`);
      return;
    }
    init();
  }, []);

  return (
    <PageContainer
      header={
        <Breadcrumb
          items={[
            {
              title: '离退休信息管理',
              onClick: e => {
                navigate(`${getPathRoot()}/nonemployed/retire`);
              },
            },
            { title: '离退休教职工查看' },
          ]}
        />
      }
    >
      <div className={styles.base_info}>
        <Basic isEdit={false} buttonList={[<Button onClick={() => navigate(-1)}>返回</Button>]} form={form} />
      </div>
    </PageContainer>
  );
};

export default Groups;
